#include "opedb.h"
#include<QMessageBox>
#include<QDebug>
#include<QVariant>
OpeDB::OpeDB(QObject *parent) : QObject(parent)
{
    m_db = QSqlDatabase::addDatabase("QSQLITE");
}

OpeDB &OpeDB::getInstance()
{
    static OpeDB instance;
    return instance;

}

void OpeDB::init()
{
    m_db.setHostName("localhost");//localhost为本地ip地址，如果远程ip，则改为具体ip地址
    m_db.setDatabaseName("cloud.db"); //操作的数据库的名字
    if(m_db.open()){
        QSqlQuery query;
        query.exec("select * from usrInfo");
        while (query.next()) {
            //QString.arg()需包含头文件<QVariant>
            QString data = QString("%1,%2,%3").arg(query.value(0).toString()).
                    arg(query.value(1).toString()).arg(query.value(2).toString());
            qDebug()<<data;
        }
    }
    else{
        QMessageBox::critical(NULL,"打开数据库","打开数据库失败！");
    }
}

OpeDB::~OpeDB()
{
    m_db.close();
}

bool OpeDB::handleRegist(const char *name, const char *pwd)
{
    if(NULL == name || NULL == pwd){
        return false;
    }
    QString data = QString("insert into usrInfo(name,pwd) values(\'%1\',\'%2\')").
            arg(name).arg(pwd);
    QSqlQuery query;
    //如果数据插入成功，则返回真
    return query.exec(data);
}

bool OpeDB::handleLogin(const char *name, const char *pwd)
{
    if(NULL == name || NULL == pwd){
        qDebug()<<"name | pwd is NULL";
        return false;
    }
    QString data = QString("select *from usrInfo where name=\'%1\' and pwd=\'%2\' and "
                           "online=0").arg(name).arg(pwd);
    QSqlQuery query;
    query.exec(data);
    //如果数据库中存在要查找的数据，则执行.next语句返回true,否则返回false，
    //每执行一次next就筛查数据库中的一条数据，将筛查到符合查询条件的结果存到query对象中
    if(query.next())
    {
        data = QString("update usrInfo set online=1 where name=\'%1\' and pwd=\'%2\'"
                       ).arg(name).arg(pwd);
            QSqlQuery query;
            query.exec(data);
            return true;
    }
    else
    {
        return false;
    }
}

void OpeDB::handleOffline(const char *name)
{
    if(NULL == name ){
        qDebug()<<"name is NULL";
        return;
    }
    QString data = QString("update usrInfo set online=0 where name=\'%1\'"
                           ).arg(name);
    QSqlQuery query;
    query.exec(data);
}

QStringList OpeDB::handleAllOnline()
{
    QString data = QString("select name from usrInfo where online=1");
    QSqlQuery query;
    query.exec(data);
    QStringList result;
    result.clear();

    while (query.next()) {
        result.append(query.value(0).toString());
    }
    return result;
}

int OpeDB::handleSearchUsr(const char *name)
{
    if(NULL == name)
    {
        return -1;
    }
    QString data = QString("select online from usrInfo where name=\'%1\'").arg(name);
    QSqlQuery query;
    query.exec(data);
    if(query.next())
    {
        int ret = query.value(0).toInt();
        if(1 == ret)
        {
            return 1;
        }
        else if(0 == ret)
        {
            return 0;
        }
    }
    else
    {
        return -1;
    }
}

int OpeDB::handleAddFriend(const char *pername, const char *name)
{
    if(NULL == pername || NULL == name)
    {
        return -1;
    }
    QString data = QString("select *from friend where (id=(select id from usrInfo where name=\'%1\') "
                           "and friendId = (select id from usrInfo where name=\'%2\'))"
                           "or (id=(select id from usrInfo where name=\'%3\') "
                           "and friendId = (select id from usrInfo where name=\'%4\'))")
                            .arg(pername).arg(name).arg(name).arg(pername);
    qDebug()<<data;
    QSqlQuery query;
    query.exec(data);
    if(query.next())
    {
        return 0;//如果query.next()返回true，则表示查询到了满足条件的数据，说明双方已经是好友了
    }
    else
    {  //pername表示正要添加的好友的名字
        //查看要添加的好友是否在线
        data = QString("select online from usrInfo where name= \'%1\'").arg(pername);
        QSqlQuery query;
        query.exec(data);
        if(query.next())
        {
            int ret = query.value(0).toInt();
            if(1 == ret)
            {
                return 1;  //好友在线
            }
            else if(0 == ret) //好友不在线
            {
                return 2;
            }
        }
        else //数据库中不存在名字为pername的数据则返回3
        {
            return 3;
        }
    }
}

QStringList OpeDB::handleFlushFriend(const char *name)
{
    QStringList strFriendList;
    strFriendList.clear();
    if(NULL == name)
    {
        return strFriendList;
    }
    QString data = QString("select name from usrInfo where online=1 "
                           "and id in (select id from friend where "
                           "friendId=(select id from usrInfo where name=\'%1\'))").arg(name);
    QSqlQuery query;
    query.exec(data);
    while (query.next()) {
        strFriendList.append(query.value(0).toString());
    }

    data = QString("select name from usrInfo where online=1 "
                               "and id in (select friendId from friend where "
                             "id=(select id from usrInfo where name=\'%1\'))").arg(name);
    query.exec(data);
    while (query.next()) {
        strFriendList.append(query.value(0).toString());
    }
    return strFriendList;
}

bool OpeDB::handleDelFriend(const char *name, const char *friendName)
{
    if(NULL == name || NULL == friendName)
    {
        return false;
    }
    QString data = QString("delete from friend where "
                           "id=(select id from usrInfo where name=\'%1\') and "
                           "friendId=(select id from usrInfo where name=\'%2\')")
                            .arg(name).arg(friendName);
    QSqlQuery query;
    query.exec(data);

    data = QString("delete from friend where "
                           "id=(select id from usrInfo where name=\'%1\') and "
                           "friendId=(select id from usrInfo where name=\'%2\')")
                            .arg(friendName).arg(name);
    query.exec(data);
    return true;
}



